# -*- coding: utf-8 -*-
import sys

from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class WidgetFuelIndicator(QWidget):
    def __init__(self, *args, **kwargs):
        text = kwargs.pop("text", "")
        super(WidgetFuelIndicator, self).__init__(*args, **kwargs)
        self.TANK_AT_LEFT = 1
        self.TANK_AT_RIGHT = 2
        self.TANK_CLOSED = 0
        self.InitUI()

    def InitUI(self):
        self.cuttentTank = self.TANK_CLOSED
        self.enable_color = QColor(0, 255, 0)  # 使能状态
        self.warn_color = QColor(255, 255, 0)  # 告警提示状态
        self.critical_color = QColor(255, 0, 0)  # 失效告警状态
        self.disable_color = QColor(200, 200, 200) # 关闭状态
        self.warn_level = 0
        self.bgcolor_set = QColor(50, 50, 50, 0)

    def paintEvent(self, event):
        width = self.width()
        height = self.height()

        painter = QPainter(self)  # 初始化painter
        painter.translate(width / 2, height / 2)  # 坐标轴变换，调用translate()将坐标原点平移至窗口中心
        painter.setRenderHints(QPainter.Antialiasing | QPainter.TextAntialiasing)

        self.bgWidth = width
        self.bgHeight = height

        painter.begin(self)

        # 画出背景区域
        painter.setPen(Qt.NoPen)
        brush = QBrush(Qt.SolidPattern)
        brush.setColor(self.bgcolor_set)
        painter.setBrush(brush)
        painter.drawRect(-self.bgWidth / 2, -self.bgHeight / 2, self.bgWidth, self.bgHeight)

        # 左油箱绘制
        if self.cuttentTank == self.TANK_AT_LEFT:
            if self.warn_level == 0:
                brush.setColor(self.enable_color)
            elif self.warn_level == 1:
                brush.setColor(self.warn_color)
            else:
                brush.setColor(self.critical_color)
        else:
            brush.setColor(self.disable_color)

        painter.setBrush(brush)
        pts = QPolygon()
        pts.setPoints(width * (-0.1), height * (-0.4),
                      width * (-0.1), height * (-0.5),
                      width * (-0.5), height * 0.4,
                      width * (-0.45), height * 0.5)
        painter.drawConvexPolygon(pts)

        # 右油箱标识标识绘制
        if self.cuttentTank == self.TANK_AT_RIGHT:
            if self.warn_level == 0:
                brush.setColor(self.enable_color)
            elif self.warn_level == 1:
                brush.setColor(self.warn_color)
            else:
                brush.setColor(self.critical_color)
        else:
            brush.setColor(self.disable_color)
        painter.setBrush(brush)
        pts = QPolygon()
        pts.setPoints(width * 0.1, height * (-0.4),
                      width * 0.1, height * (-0.5),
                      width * 0.5, height * 0.4,
                      width * 0.45, height * 0.5)
        painter.drawConvexPolygon(pts)
        if self.warn_level == 0:
            tmp_color = self.enable_color
        elif self.warn_level == 1:
            tmp_color = self.warn_color
        else:
            tmp_color = self.critical_color

        painter.setPen(QPen(tmp_color, 1, Qt.SolidLine))
        painter.setFont(QFont("Noto Sans S Chinese Bold", 12))
        if self.cuttentTank == self.TANK_AT_LEFT:
            str_value = "左油箱"
        elif self.cuttentTank == self.TANK_AT_RIGHT:
            str_value = "右油箱"
        else:
            str_value = "油箱关闭"

        text_width = painter.fontMetrics().width(str_value) * 1.2
        text_height = painter.fontMetrics().height()
        rectf = QRectF(-text_width / 2, - text_height / 2, text_width, text_height)
        painter.translate(0, height * 0.2)
        painter.drawText(rectf, Qt.AlignVCenter | Qt.AlignCenter, str_value)
        painter.end()

    def setCuttentTank(self, tankid):
        '''
        设置油箱位置

        :param tankid: tankid: 0-油箱关闭，1-左油箱，2-右油箱
        :return:
        '''
        if tankid == 1:
            self.cuttentTank = self.TANK_AT_LEFT
        elif tankid == 2:
            self.cuttentTank = self.TANK_AT_RIGHT
        else:
            self.cuttentTank = self.TANK_CLOSED
        self.update()

    def setCurrentWarnLevel(self, warn_level):
        self.warn_level = warn_level
        self.update()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = WidgetFuelIndicator()
    demo.setGeometry(100, 100, 400, 200)
    demo.show()

    demo.setCuttentTank(0)

    # demo.setIntValue(6)  # 设置指针指向
    # demo.setFloatValue(5.5)
    # demo.setTitle("Title_____")  # 设置图标题
    sys.exit(app.exec_())
